package com.pfs.internal.timestamp.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;

import org.apache.commons.lang.StringUtils;

public class Formater {
	private static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	private static final SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy");
	private static DecimalFormat df = (DecimalFormat) NumberFormat.getNumberInstance(Locale.FRENCH);
	private static DecimalFormat ff = new DecimalFormat("#.##########");
	static {
		DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
		dfs.setGroupingSeparator(',');
		dfs.setDecimalSeparator('.');
	}

	public static Date str2date(String str) throws Exception {
		if (StringUtils.isBlank(str))
			return null;
		Date result = null;
		try {
			result = sdf.parse(str);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	public static Date str2date2(String str) throws Exception {
		if (StringUtils.isBlank(str))
			return null;
		Date result = null;
		try {
			result = sdf2.parse(str);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	public static Calendar str2cal(String str) throws Exception {
		Date date = str2date(str);
		if (date == null)
			return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	public static java.sql.Date cal2date(Calendar cal) throws Exception {
		Date date = cal.getTime();
		return new java.sql.Date(date.getTime());
	}

	public static String cal2str(Calendar cal) throws Exception {
		Date date = cal.getTime();
		return date2str(date);
	}

	public static String cal2yyyymm(Calendar cal) throws Exception {
		int month = cal.get(Calendar.MONTH) + 1;
		if (month < 10)
			return cal.get(Calendar.YEAR) + "0" + month;
		else
			return cal.get(Calendar.YEAR) + "" + month;
	}

	public static String cal2yyyyqq(Calendar cal) throws Exception {
		int mm = cal.get(Calendar.MONTH);
		return cal.get(Calendar.YEAR) + "0" + ((mm / 3) + 1);
	}

	public static String date2str(Date d) {
		if (d == null)
			return null;
		return sdf.format(d);
	}

	public static java.sql.Date str2sqldate(String str) throws Exception {
		if (StringUtils.isBlank(str))
			return null;
		return new java.sql.Date(str2date(str).getTime());
	}

	public static String sqldate2str(java.sql.Date d) {
		return date2str((Date) d);
	}

	public static String num2str(double number) {
		String temp = ff.format(number);
		if (StringUtils.isNotBlank(temp) && temp.indexOf(".") >= 0) {
			if (new Integer(temp.substring(temp.indexOf(".") + 1)).intValue() == 0)
				return temp.substring(0, temp.indexOf("."));
		}
		return temp;
	}

	public static String num2str(long number) {
		return df.format(number);
	}

	public static Number str2num(String str) throws Exception {
		if (StringUtils.isBlank(str))
			return null;
		str = str.replaceAll(" ", "");
		Number result = null;
		try {
			result = df.parse(str);
		} catch (Exception e) {
			throw new Exception(e.toString());
		}
		return result;
	}

	public static Number str2double(String str) throws Exception {
		if (StringUtils.isBlank(str))
			return null;
		str = str.replaceAll(" ", "");
		Number result = null;
		try {
			result = ff.parse(str);
		} catch (Exception e) {
			throw new Exception(e.toString());
		}
		return result;
	}

	// Dinh dang du lieu Date voi format truyen vao
	public static String getFormatDate(Date date, String format) {
		DateFormat formatter = new SimpleDateFormat(format);
		String temp = formatter.format(date);
		return temp;
	}

	// Format so tien - thong bao thue
	public static String formatMoney(double dbvalue) {
		NumberFormat formatter = DecimalFormat.getInstance();
		String strValue = formatter.format(dbvalue);
		return strValue;
	}

	/**
	 * 
	 * Thu tuc lay ngay cuoi thang, quy, nam cua Date truyen vao
	 * 
	 * @param Date
	 *            : Ngay can lay ngay cuoi Datescope nhan 3 gia tri: "M", "Q",
	 *            "Y" pham vi can lay ngay cuoi
	 * @return Date ngay cuoi thang, quy, nam cua thang truyen vao tuy thuoc vao
	 *         gia tri cua Datescope la "M", "Q" hay "Y"
	 */
	public static Date getLastday(Date datein, String Datescope) {

		Calendar c = Calendar.getInstance();
		c.setTime(datein);
		if (Datescope.equalsIgnoreCase("M")) {
			c.set(Calendar.DATE, 1);
			c.add(Calendar.MONTH, 1);
			c.add(Calendar.DATE, -1);
		} else if (Datescope.equalsIgnoreCase("Q")) {
			int m = c.get(Calendar.MONTH);
			if (m <= Calendar.MARCH)
				c.set(Calendar.MONTH, Calendar.MARCH);
			else if (m <= Calendar.JUNE)
				c.set(Calendar.MONTH, Calendar.JUNE);
			else if (m <= Calendar.SEPTEMBER)
				c.set(Calendar.MONTH, Calendar.SEPTEMBER);
			else
				c.set(Calendar.MONTH, Calendar.DECEMBER);

			c.set(Calendar.DATE, 1);
			c.add(Calendar.MONTH, 1);
			c.add(Calendar.DATE, -1);
		} else if (Datescope.equalsIgnoreCase("Y")) {
			c.set(Calendar.DATE, 31);
			c.set(Calendar.MONTH, Calendar.DECEMBER);
		} else
			return null;

		Date returnDate = new Date(c.getTime().getTime());
		return returnDate;
	}

	public static String date2quarter(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return String.valueOf(c.get(Calendar.MONTH) / 3 + 1) + "/" + c.get(Calendar.YEAR);
	}
}